package com.gdpu.project.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.dao.ProApplyDao;
import com.gdpu.project.dao.ProApprovalDao;
import com.gdpu.project.dao.ProEndDao;
import com.gdpu.project.dao.ProMonitorDao;
import com.gdpu.project.dao.ProjectDao;
import com.gdpu.project.dao.TeacherDao;
import com.gdpu.project.service.ProjectService;
import com.gdpu.project.vo.ProApply;
import com.gdpu.project.vo.ProApproval;
import com.gdpu.project.vo.ProEnd;
import com.gdpu.project.vo.ProMonitor;
import com.gdpu.project.vo.Project;
import com.gdpu.project.vo.Teacher;

public class ProjectServiceImpl implements ProjectService {
	private ProjectDao projectDao;
	private ProApplyDao proApplyDao;
	private ProApprovalDao proApprovalDao;
	private ProMonitorDao proMonitorDao;
	private ProEndDao proEndDao;
	private TeacherDao teacherDao;
	
	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public ProApplyDao getProApplyDao() {
		return proApplyDao;
	}

	public void setProApplyDao(ProApplyDao proApplyDao) {
		this.proApplyDao = proApplyDao;
	}

	public ProApprovalDao getProApprovalDao() {
		return proApprovalDao;
	}

	public void setProApprovalDao(ProApprovalDao proApprovalDao) {
		this.proApprovalDao = proApprovalDao;
	}

	public ProMonitorDao getProMonitorDao() {
		return proMonitorDao;
	}

	public void setProMonitorDao(ProMonitorDao proMonitorDao) {
		this.proMonitorDao = proMonitorDao;
	}

	public ProEndDao getProEndDao() {
		return proEndDao;
	}

	public void setProEndDao(ProEndDao proEndDao) {
		this.proEndDao = proEndDao;
	}

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public Boolean addProject(Project project) {
		if (project != null) {
			projectDao.save(project);
			return true;
		}
		return false;
	}

	public Boolean updateProject(Project project) {
		if (project != null) {
			projectDao.merge(project);
			return true;
		}
		return false;
	}

	public Boolean deleteProjectByProjectId(int id) {
		Project project = projectDao.findById(id);
		if (project != null) {
			projectDao.delete(project);
			return true;
		}
		return false;
	}

	public Project findProjectById(int projectId) {
		return  projectDao.findById(projectId);
	}

	public List<Project> findAllProject() {
		List<Project> projectlist = projectDao.findAll();
		if (projectlist.size() > 0) {
			return projectlist;
		}
		return null;
	}

	public QueryResult<Project> findProjectByType(String projectType,int firstindex,int maxresult) {
		QueryResult<Project> queryResult = projectDao.findByProjectType(projectType, firstindex, maxresult);
		return queryResult;
	}

	public Boolean addProApply(ProApply proapply) {
		if (proapply != null) {
			proApplyDao.save(proapply);
			return true;
		}
		return false;
	}

	public Boolean deleteProApply(ProApply proapply) {
		if (proapply != null) {
			proApplyDao.delete(proapply);
			return true;
		}
		return false;
	}

	public Boolean updateProApply(ProApply proapply) {
		if (proapply != null) {
			proApplyDao.merge(proapply);
			return true;
		}
		return false;
	}

	public List<ProApply> findAllProApply() {
		List<ProApply> list = proApplyDao.findAll();
		if (list.size() > 0) {
			return list;
		}
		return null;
	}
	
	
	public Map<ProApply, Teacher> findProApply(String projectId) {
		Map<ProApply, Teacher> map = new HashMap();
		List<ProApply> list = proApplyDao.findByProjectId(projectId);
		for (int i = 0; i < list.size(); i++) {
			ProApply proApply = list.get(i);
			String teacherId = list.get(i).getTeacherId();
			Teacher teacher = teacherDao.findById(teacherId);
			map.put(proApply, teacher);
		}
		return map;
	}

	public Map<ProApproval, Teacher> findProApproval(String projectId){
		Map<ProApproval, Teacher> map = new HashMap();
		List<ProApproval> list = proApprovalDao.findByProjectId(projectId);
		if(list.size()>0){
			ProApproval proApproval = list.get(0);
			String teacherId = list.get(0).getTeacherId();
			Teacher teacher = teacherDao.findById(teacherId);
			map.put(proApproval, teacher);
			return map;
		}
		return null;
	}
	
	public Map<ProMonitor, Teacher> findProMonitor(String projectId){
		Map<ProMonitor, Teacher> map = new HashMap();
		List<ProMonitor> list = proMonitorDao.findByProjectId(projectId);
		for (int i = 0; i < list.size(); i++) {
			ProMonitor proMonitor = list.get(i);
			String teacherId = list.get(i).getTeacherId();
			Teacher teacher = teacherDao.findById(teacherId);
			map.put(proMonitor, teacher);
		}
		return map;
	}
	
	public Map<ProEnd, Teacher> findProEnd(String projectId){
		Map<ProEnd, Teacher> map = new HashMap();
		List<ProEnd> list = proEndDao.findByProjectId(projectId);
		for (int i = 0; i < list.size(); i++) {
			ProEnd proEnd = list.get(i);
			String teacherId = list.get(i).getTeacherId();
			Teacher teacher = teacherDao.findById(teacherId);
			map.put(proEnd, teacher);
		}
		return map;
	}
	
	

	public List<ProApply> findAllProApplyByTeacherId(String teacherId) {
		List<ProApply> list = proApplyDao.findByTeacherId(teacherId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List<ProApply> findAllProApplyByProjectId(String projectId) {
		List<ProApply> list = proApplyDao.findByProjectId(projectId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public Boolean addProApproval(ProApproval proApproval) {
		proApprovalDao.save(proApproval);
		return true;
	}

	public Boolean deleteProApproval(ProApproval proApproval) {
		proApprovalDao.delete(proApproval);
		return true;
	}

	public Boolean updateProApproval(ProApproval proApproval) {
		proApprovalDao.merge(proApproval);
		return true;
	}

	public List<ProApproval> findAllProApproval() {
		List<ProApproval> list = proApprovalDao.findAll();
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List<ProApproval> findAllProApprovalByProjectId(String projectId) {
		List<ProApproval> list = proApprovalDao.findByProjectId(projectId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List<ProApproval> findAllProApprovalByTeacherId(String teacherId) {
		List<ProApproval> list = proApprovalDao.findByTeacherId(teacherId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public Boolean addProMonitor(ProMonitor proMonitor) {
		proMonitorDao.save(proMonitor);
		return true;
	}

	public Boolean updateProApproval(ProMonitor proMonitor) {
		proMonitorDao.merge(proMonitor);
		return true;
	}

	public Boolean deleteProMonitor(ProMonitor proMonitor) {
		proMonitorDao.delete(proMonitor);
		return true;
	}

	public List<ProMonitor> findAllProMonitor() {
		List<ProMonitor> list = proMonitorDao.findAll();
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List<ProMonitor> findAllProMonitorByTeacherId(String teacherId) {
		List<ProMonitor> list = proMonitorDao.findByTeacherId(teacherId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List<ProMonitor> findAllProMonitorlByProjectId(String projectId) {
		List<ProMonitor> list = proMonitorDao.findByProjectId(projectId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public Boolean addProEnd(ProEnd proEnd) {
		proEndDao.save(proEnd);
		return true;
	}

	public Boolean deleteProEnd(ProEnd proEnd) {
		proEndDao.delete(proEnd);
		return true;
	}

	public List<ProEnd> findAllProEnd() {
		List<ProEnd> list = proEndDao.findAll();
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List<ProEnd> findAllProEndByProjectId(String projectId) {
		List<ProEnd> list = proEndDao.findByProjectId(projectId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List<ProEnd> findAllProEndByTeacherId(String teacherId) {
		List<ProEnd> list = proEndDao.findByTeacherId(teacherId);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public Boolean updateProEnd(ProEnd proEnd) {
		proEndDao.merge(proEnd);
		return true;
	}
	
	public QueryResult<Project> findByTeacherId(String teacherId,int firstindex,int maxresult){
		return projectDao.findByTeacherId(teacherId, firstindex, maxresult);
	}
}
